/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package karshafinsearch;

import edu.stanford.nlp.ling.HasWord;
import edu.stanford.nlp.process.DocumentPreprocessor;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
import org.pdfbox.util.PDFTextStripper;

/**
 *
 * @author lsf
 */
public class parseDocument {

     public ArrayList<String> txtDocToSentence(String paragraph) {
    // This option shows loading and sentence-segment and tokenizing
    // a file using DocumentPreprocessor
      ArrayList<String> sentenceList = new ArrayList<String>();

      //String paragraph = "My first sentence. My second sentence.";
Reader reader = new StringReader(paragraph);
//DocumentPreprocessor dp = new DocumentPreprocessor(reader);
      
      /*
    TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();

       *
       * */
    // You could also create a tokenier here (as below) and pass it
    // to DocumentPreprocessor
    for (List<HasWord> sentence : new DocumentPreprocessor(reader)) {
       //Add sentence to array List
    sentenceList.add(sentence.toString());

    //System.out.print(sentenceList.indexOf(sentence.toString()) +" "+ sentence.toString()+"\n");

    /*
    //printing the parse tree
      Tree parse = lp.apply(sentence);
      parse.pennPrint();
      System.out.println();
    // printing gramatical strucure
      GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
      Collection tdl = gs.typedDependenciesCCprocessed(true);
      System.out.println(tdl);
      System.out.println();
      */
    }
    return sentenceList;
  }

     
  public String show(){
  return "its working";
  }
     
//public ArrayList<String> pdfDocToSentence(String fileName) {
public ArrayList<String> pdfDocToSentence(byte[] buf ) {

    PDFParser parser;
    String parsedText;
    String txtfilename = null;
    PDFTextStripper pdfStripper;
    PDDocument pdDoc = null ;
    COSDocument cosDoc = null ;
    PDDocumentInformation pdDocInfo;
    ArrayList<String> sentenceList = new ArrayList<String>();

        //System.out.println("Parsing text from PDF file " + fileName + "....");
       // File f = new File(fileName);
       // ByteArrayInputStream f1=new ByteArrayInputStream(buf);
//File f = new File(f1);
//        if (!f.isFile()) {
//            System.out.println("File  does not exist.");
//            return null;
//        }

        try {
            parser = new PDFParser(new ByteArrayInputStream(buf));   
           // parser = new PDFParser(new FileInputStream(f));
        } catch (Exception e) {
            System.out.println("Unable to open PDF Parser.");
            return null;
        }

        try {
            parser.parse();
            cosDoc = parser.getDocument();
            pdfStripper = new PDFTextStripper();
            pdDoc = new PDDocument(cosDoc);
            parsedText = pdfStripper.getText(pdDoc);
            //System.out.println("An exception occured in parsing the PDF Document.");
        } catch (Exception e) {
            System.out.println("An exception occured in parsing the PDF Document.");
            e.printStackTrace();
            try {
                   if (cosDoc != null) cosDoc.close();
                   if (pdDoc != null) pdDoc.close();
               } catch (Exception e1) {
               e.printStackTrace();
            }
            return null;
        }
        System.out.println("Done.");

         //PDFTextParser pdfTextParserObj = new PDFTextParser();
        //String pdfToText = pdfTextParserObj.pdftoText(inputfile);

        if (parsedText == null) {
        	System.out.println("PDF to Text Conversion failed.");
        }
        else {
        	//System.out.println("\nThe text parsed from the PDF Document....\n" + parsedText);
       // txtfilename= writeTexttoFile(parsedText, fileName.concat(".txt"));
        //sentenceList= txtDocToSentence(txtfilename);
        }

return txtDocToSentence(parsedText);
        // return txtDocToSentence(txtfilename);
        //return parsedText;
    }

 public String writeTexttoFile(String text, String fileName) {

    	System.out.println("\nWriting PDF text to output text file " + fileName+"...");
    	try {

            PrintWriter pw = new PrintWriter(fileName);
    		pw.print(text);
    		pw.close();
    	} catch (Exception e) {
    		System.out.println("An exception occured in writing the pdf text to file.");
    		e.printStackTrace();
    	}
    	System.out.println("Done.");

        return fileName;
    }
}
